using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HermesPortal.Core;  
public partial class Realms_Controls_RealmEdit : System.Web.UI.UserControl
{
    private int _sagaId = 0;
    private int _realmID = 0;
    public int SagaID
    {
        get { return _sagaId; }
        set { _sagaId = value; }
    }
    public int RealmID
    {
        get { return _realmID; }
        set { _realmID = value; }
    }

    public int CreatorID
    {
        get { return ((PageBase)this.Page).Creator.Id; }
    }
    public bool IsNewRealm
    {
        get { return (RealmID == 0); }
    }
  
    protected void Page_Load(object sender, EventArgs e)
    {



        string sPath = "~/fckeditor/";

      

        string imageBrowser = sPath + "editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/aspx/connector.aspx";
        string linkBrowserUrl = sPath + "editor/filemanager/browser/default/browser.html?Connector=connectors/aspx/connector.aspx";

        fckSummary.BasePath = sPath;
        fckSummary.ImageBrowserURL = imageBrowser;
        fckSummary.LinkBrowserURL = linkBrowserUrl;
        fckSummary.PluginsPath = sPath + "plugins/";

        fckDescription.BasePath = sPath;
        fckDescription.ImageBrowserURL = imageBrowser;
        fckDescription.LinkBrowserURL = linkBrowserUrl;
        fckDescription.PluginsPath = sPath + "plugins/";

        ddlRealms.DataValueField = "key";
        ddlRealms.DataTextField = "value";
        ddlRealms.DataSource = RealmServices.GetRealmSubNodesInfo(this.SagaID, RealmID, ((PageBase)this.Page).Creator.Id);
        ddlRealms.DataBind();
        ddlRealms.Items.Insert(0, new ListItem("Root Realm", "0"));

    }
  

    #region private helpers

    public void BindControls()
    {

        if (! IsNewRealm )
        {
            Realm realm = RealmServices.GetRealm(RealmID);
            txtTitle.Text = realm.Title;
            fckSummary.Value = realm.Summary;
            fckDescription.Value = realm.Description;
            chkIsPrivate.Checked = realm.IsPrivate;
            ListItem liRealm = ddlRealms.Items.FindByValue(realm.ParentID.ToString());
            if (liRealm != null)
                liRealm.Selected = true;
        }
       
    }

    /// <summary>
    /// Save Realm changes 
    /// </summary>
    public void SaveRealmChanges()
    {
        Realm realm = new Realm() ;
        if (!IsNewRealm) 
            realm = RealmServices.GetRealm(RealmID);
        realm.SagaID = SagaID; 
        realm.Title = txtTitle.Text;   
        realm.ParentID =  Convert.ToInt32( ddlRealms.SelectedValue );
        realm.Summary = fckSummary.Value;
        realm.Description = fckDescription.Value;   
        realm.IsPrivate = chkIsPrivate.Checked;
        if (IsNewRealm)
         RealmID =  RealmServices.AddRealm(realm);
        else
            RealmServices.UpdateRealm(realm);  
        BindControls();
    }
#endregion 
}
